package com.langshixiaobai.types.design.framework.tree;

import lombok.Getter;
import lombok.Setter;

//通过调用策略映射器get方法，控制节点流程的走向。
public abstract class AbstractStrategyRouter<T, D, R> implements StrategyMapper<T, D, R>, StrategyHandler<T, D, R> {

    @Getter
    @Setter
    protected StrategyHandler<T, D, R> defaultStrategyHandler = StrategyHandler.DEFAULT;

    public R router(T requestParameter, D dynamicContext) throws Exception {
        // 获取策略
        StrategyHandler<T, D, R> strategyHandler = get(requestParameter, dynamicContext);
        //
        if(strategyHandler != null) return strategyHandler.apply(requestParameter, dynamicContext);
        // 返回null 因为strategyHandler为null,无论传入什么
        return  defaultStrategyHandler.apply(requestParameter, dynamicContext);

    }

}